package com.ubi.core.utils.converter;

import java.sql.Timestamp;
import java.util.Date;
import java.util.Map;

public class MapConverter {

    public <T> T convert(Map<String, Object> map, String key, Class<T> targetClass) {
        if (map.get(key).getClass().equals(Timestamp.class)) {
            Timestamp timestamp = (Timestamp) map.get(key);
            return (T) new Date(timestamp.getTime());
        }

        if (!map.get(key).getClass().equals(targetClass)) {
            throw new IllegalStateException("could not cast" + map.get(key).getClass().getName() + " to " + targetClass.getName());
        }
        return (T) map.get(key);
    }
}
